package com.jensen.service;

import com.jensen.dao.AddressDao;
import com.jensen.dao.UserRepository;
import com.jensen.entity.Address;
import com.jensen.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.util.List;
import java.util.UUID;

/**
 * Created by Jensen on 2016/9/28.
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AddressDao addressDao;


    @Transactional
    public String test1() {
        this.userRepository.delete("2");
        this.userRepository.save(new User(UUID.randomUUID().toString().toString(),"xxx","yyy"));
        this.userRepository.save(new User(UUID.randomUUID().toString().toString(),"xxx","yyy"));
        this.addressDao.save(new Address(UUID.randomUUID().toString(),"xxx"));
        this.userRepository.save(new User(UUID.randomUUID().toString().toString(),"xxx","yyy"));
        User u=this.userRepository.findOne("2");
        return "success";
    }

    @Transactional
    public void test2() {
        String hql="update User set name='a' where id='1'";
        this.userRepository.execByHql(hql);
        String sql="update user set user.name='b' where user.id='2'";
        this.userRepository.execBySql(sql);
        System.out.println();
        throw new RuntimeException("test2");
    }

    public void test3(){
        this.userRepository.save(new User(UUID.randomUUID().toString(),"xxx","yyy"));
        String hql="update User set name='a' where id='1'";
        this.userRepository.execByHql(hql);
        System.out.println();
    }

    @Transactional
    public void test4(){
        List<User> users=this.userRepository.selectByHql("from User");
        List addresses=this.userRepository.selectBySql("select * from address");
        System.out.println();
    }

}
